package com.code.module.funds.action;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.struts2.ServletActionContext;

import com.code.base.acc.pojo.AccPsn;
import com.code.base.busi.pojo.BusiDepList;
import com.code.base.sys.pojo.SysUser;
import com.code.module.funds.service.FundsService;
import com.code.vo.JSONVO;

public class FundsAction {

	@Resource
	private FundsService fundsService;
	
	public String fundsDep() {
		String json = ServletActionContext.getRequest().getParameter("json");
		
		JSONArray jsonArray = JSONArray.fromObject(json);
		Object[] objs = jsonArray.toArray();
		
		JSONObject jsonObject = JSONObject.fromObject(objs[0]);
		JSONObject temp = new JSONObject();
		for (Object key : jsonObject.keySet()) {
			if (null != jsonObject.get(key) && !"".equals(jsonObject.get(key))) {
				temp.put(key, jsonObject.get(key));
			}
		}
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"}));
		AccPsn accPsn = (AccPsn) JSONObject.toBean(temp, AccPsn.class);
		
		Double payMoney = Double.parseDouble(String.valueOf(objs[1]));
		String payDate = (String) objs[2];
		
		SysUser sysUser = (SysUser) ServletActionContext.getRequest().getSession().getAttribute("SYS_USER");
		
		fundsService.fundsDep(accPsn, payMoney, payDate, sysUser);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	public String fundsReq() {
		String json = ServletActionContext.getRequest().getParameter("json");
		
		JSONArray jsonArray = JSONArray.fromObject(json);
		/**
		 * 索引	0:BusiDepList数组
		 * 		1:缴存银行
		 * 		2:缴存申请日期
		 * 		3:申请有效日期
		 */
		Object[] objs = jsonArray.toArray();
		
		Object[] array = ((JSONArray) objs[0]).toArray();
		BusiDepList[] busiDepLists = new BusiDepList[array.length];
		for (int i = 0; i < array.length; i++) {
			JSONObject jsonObject = JSONObject.fromObject(array[i]);
			JSONObject temp = new JSONObject();
			for (Object key : jsonObject.keySet()) {
				if (null != jsonObject.get(key) && !"".equals(jsonObject.get(key))) {
					temp.put(key, jsonObject.get(key));
				}
			}
			JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"}));
			busiDepLists[i] = (BusiDepList) JSONObject.toBean(temp, BusiDepList.class);
		}
		
		String bank = String.valueOf(objs[1]);
		String reqDate = String.valueOf(objs[2]);
		String validDate = String.valueOf(objs[3]);
		
		fundsService.fundsReq(busiDepLists, bank, reqDate, validDate);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(new JSONVO());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	
}
